package com.cndw.kungfu.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Event;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.model.message.EventManager;
import com.cndw.kungfu.vo.EventVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 玩家事件簿
 * 
 * @author fantadust
 * @date 2011-5-13 下午02:13:50
 */
@Controller
public class EventController {
	@Autowired
	private EventManager eventManager;

	/**
	 * 事件列表
	 * 
	 * @param session
	 * @param readMap
	 * @return
	 */
	public ResponseMessage readList(Session session, Map<String, Integer> readMap) {
		long playerId = session.getUid();
		int type = readMap.containsKey("type") ? readMap.get("type") : 0;
		int page = readMap.containsKey("page") ? readMap.get("page") : 1;
		page = page <= 0 ? 1 : page;
		int pageSize = readMap.containsKey("pageSize") ? readMap.get("pageSize") : Constants.PAGE_SIZE;
		pageSize = pageSize == 0 ? Constants.PAGE_SIZE : pageSize;

		List<EventVO> list = eventManager.getList(playerId, type, (page - 1) * pageSize, pageSize, false);
		int counts = eventManager.getcount(playerId, type);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("counts", counts);

		map.put("type", type);
		map.put("page", page);
		map.put("pageSize", pageSize);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 获取批量信息
	 * 
	 * @param session
	 * @param type
	 *            0武林大会战报 1侍从系统互动信息
	 * @return
	 */
	public ResponseMessage getReport(Session session, int type) {
		long playerId = session.getUid();
		List<EventVO> list = null;
		if (type == 0) {
			list = eventManager.getList(playerId, Constants.eventTypePvp, 0, ConstantsModel.challengeReportNum, true);
		} else if (type == 1) {
			list = eventManager.getList(playerId, Constants.eventTypeLivery, 0, ConstantsModel.liveryMsgNum, true);
		} else if (type == 16) {
			list = eventManager.getList(playerId, Constants.eventTypeFruit, 0, ConstantsModel.liveryMsgNum, true);
		}
		List<String> reportList = new LinkedList<String>();
		if (list != null) {
			for (EventVO event : list) {
				reportList.add(event.content);
			}
		}
		return ResponseMessage.getSuccMessage(reportList);
	}

	/**
	 * 读一个事件
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage readEvent(Session session, int id) {

		Event event = eventManager.getEvent(session.getUid(), id);
		if (event != null) {
			eventManager.signReaded(session.getUid(), id);
			event.setReaded(true);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("event", event.toEventVO());
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 标记已读
	 * 
	 * @param session
	 * @param ids
	 * @return
	 */
	public ResponseMessage signReaded(Session session, String idsStr) {
		int[] ids = StringProcesser.toIntArray(idsStr);
		if (ids == null || ids.length <= 0)
			return null;
		eventManager.signReaded(session.getUid(), ids);
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 保存一个事件
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage saveEvent(Session session, int id) {
		eventManager.saveEvent(session.getUid(), id);
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 删除事件
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage delEvent(Session session, int id) {
		eventManager.delEvent(session.getUid(), id);
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 删除一组事件
	 * 
	 * @param session
	 * @param ids
	 * @return
	 */
	public ResponseMessage delListEvnet(Session session, String idsStr) {
		int[] ids = StringProcesser.toIntArray(idsStr);
		if (ids == null || ids.length <= 0)
			return null;
		eventManager.delEvent(session.getUid(), ids);
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 全部删除,带类型
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage delAllEvnet(Session session, int type) {
		eventManager.delAll(session.getUid(), type);
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

}
